package com.bw.module_mine.viewmodel

import androidx.lifecycle.viewModelScope
import com.bw.lib_common.base.BaseViewModel
import com.bw.module_mine.intent.MineIntent
import com.bw.module_mine.model.repository.MineRepository
import com.bw.module_mine.state.AccountState
import com.bw.module_mine.state.LoginState
import com.bw.module_mine.state.RegisterState
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.consumeAsFlow
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class MineViewModel @Inject constructor(private val repository: MineRepository) :
    BaseViewModel<MineIntent>() {

    private var loginState = MutableStateFlow<LoginState>(LoginState.Init)
    val loginState_ = loginState.asStateFlow()

    private var registerState = MutableStateFlow<RegisterState>(RegisterState.Init)
    val registerState_ = registerState.asStateFlow()

    private var getAccountState = MutableStateFlow<AccountState>(AccountState.Init)
    val AccountState_ = getAccountState.asStateFlow()

    override fun handleIntent() {
        viewModelScope.launch {
            intents.consumeAsFlow().collect(){
                when(it){
                    is MineIntent.Login -> {
                        login(it)
                    }
                    is MineIntent.Register -> {
                        register(it)
                    }
                    is MineIntent.GetAccount -> {
                        getAccount(it)
                    }
                }
            }
        }
    }

    private fun getAccount(intent: MineIntent.GetAccount) {
        viewModelScope.launch {
            repository.getAccount(intent.userId)
                .catch {
                    getAccountState.value = AccountState.Error
                }.filter {
                    it != null
                }.collect(){
                    if (it.code == 200) {
                        if (it.data != null) {
                            getAccountState.value = AccountState.Success(it.data)
                        }else{
                            getAccountState.value = AccountState.Empty
                        }
                    }else{
                        getAccountState.value = AccountState.Error
                    }
                }
        }
    }

    private fun login(intent: MineIntent.Login) {
        viewModelScope.launch {
            repository.login(intent.username,intent.password)
                .catch {
                    loginState.value = LoginState.Error
                }.filter {
                    it != null
                }.collect(){
                    if (it.code == 200){
                        if (it.data != null){
                            loginState.value = LoginState.Success(it.data)
                        }else{
                            loginState.value = LoginState.Empty
                        }
                    }else{
                        loginState.value = LoginState.Error
                    }
                }
        }
    }

    private fun register(intent: MineIntent.Register) {
        viewModelScope.launch {
            repository.register(intent.username,intent.password)
                .catch {
                    registerState.value = RegisterState.Error
                }
                .filter {
                    it != null
                }.collect(){
                    if (it.code == 200) {
                        if (it.data != null) {
                            registerState.value = RegisterState.Success(it.data)
                        }else{
                            registerState.value = RegisterState.Empty
                        }
                    }else{
                        registerState.value = RegisterState.Error
                    }
                }
        }
    }
}